package atm;
import java.util.Scanner;

public class diancan {
    
	private static Scanner scanner = new Scanner(System.in);
    
    private static Object[][] huiyuan= {
        {"jx01", 500, 200, true},
        {"jx02", 300, 300, false}
    };
	
    private static Object[][] mean = {
            {"m01", "素颜锡兰", 18},
            {"m02", "抹茶菩提", 22},
            {"m03", "蔓越阑珊", 25},
            {"m04", "桂花弄", 28},
            {"m05", "芊芊马卡", 20}
        };
    private static Object[][] liwu = {
            {"s01", "茶颜抽纸", 20},
            {"s02", "小小忻杯", 50},
            {"s03", "自拍杆", 100},
            {"s04", "iphone", 55},
            {"s05", "红富士", 60}
        };


    public static void main(String[] args) {
        showWelcomeMenu();
        int choice = getChoice();
        if (choice == 1) {
            login();
        } else if (choice == 2) {
            exitSystem();
        }
    }
    private static void showWelcomeMenu() {
        System.out.println("悦茶点餐系统");
        System.out.println("1，登录系统");
        System.out.println("2，退出");
        System.out.print("请选择，请输入数字：");
    }

    private static int getChoice() {
        return scanner.nextInt();
    }

    private static void login() {
        String username = getUsername();
        String password = getPassword();
        String cardCode = getCardCode();
        if (username.equals("atm") && password.equals("123456") && cardCode.equals("x20")) {
            showMainMenu();
            int choice = getChoice();
            if (choice == 1) {
                memberManagement();
            } else if (choice == 2) {
                xhz();
            } else if (choice == 3) {
                checkout();
            } else if (choice == 4) {
                pointsConsumption();
            } else if (choice == 5) {
                logout();
            }
        } else {
            System.out.println("登录失败！");
        }
    }

    private static String getUsername() {
        System.out.print("请输入账号：");
        return scanner.next();
    }

    private static String getPassword() {
        System.out.print("请输入密码：");
        return scanner.next();
    }

    private static String getCardCode() {
        System.out.print("请输入使用的储值卡代号：");
        return scanner.next();
    }

    private static void showMainMenu() {
    	while(true) {
    		System.out.println("登录成功！");
            System.out.println("悦茶点餐系统->主菜单");
            System.out.println("1,会员管理2，菜单管理3,点单管理");
            System.out.println("4，积分消费");
            System.out.println("5,注销");
            System.out.print("请选择，请输入数字：");
            
            int choice = getChoice();
            if (choice == 1) {
                memberManagement();
            } else if (choice == 2) {
                xhz();
            } else if (choice == 3) {
                checkout();
            } else if (choice == 4) {
                pointsConsumption();
            } else if (choice == 5) {
                logout();
                break;}
    	}
        
    }

    private static void memberManagement() {
        
    	System.out.println("悦茶点餐系统->主菜单->会员管理");
    	System.out.println("1.显示所有会员信息");
    	System.out.println("2.添加会员");
    	System.out.println("3.修改会员");
    	System.out.println("4.返回主菜单");
    	System.out.println("请选择，请输入数字");
    	int choice = getChoice();
    	 if (choice == 1) {
             atm1();
         } else if (choice == 2) {
            atm2();
         } else if (choice == 3) {
             atm3();
         } else if (choice == 4) {
             showMainMenu();
         }
    }
    private static void atm1() {
        System.out.println("悦茶点餐系统->主菜单->会员管理->显示所有会员信息");
        System.out.println("会员名\t储值余额\t会员积分\t是否是会员");
        for (Object[] member :huiyuan) {
            System.out.println(member[0] + "\t" + member[1] + "\t" + member[2] + "\t" + ((boolean) member[3] ? "是" : "否"));
        }
        System.out.println("回车返回上一级》》》");
        scanner.nextLine();
        memberManagement();
    }
    private static void atm2() {
        System.out.println("悦茶点餐系统->主菜单->会员管理->添加会员");
        System.out.print("请输入会员名：");
        String name = scanner.next();
        System.out.print("请输入储值余额：");
        int balance = scanner.nextInt();
        System.out.print("请输入会员积分：");
        int points = scanner.nextInt();
        System.out.print("是否是会员（是/否）：");
        boolean isMember = scanner.next().equals("是");

        Object[][] newhuiyuan = new Object[huiyuan.length + 1][4];
        for (int i = 0; i <huiyuan.length; i++) {
            newhuiyuan[i] =huiyuan[i];
        }
        newhuiyuan[huiyuan.length] = new Object[]{name, balance, points, isMember};
       huiyuan= newhuiyuan;

        System.out.println("添加成功！");
        System.out.println("回车返回上一级》》》");
        scanner.nextLine();
        memberManagement();
    }
    private static void atm3() {
        System.out.println("悦茶点餐系统->主菜单->会员管理->修改会员");
        System.out.print("请输入要修改的会员名：");
        String name = scanner.next();

        int index = -1;
        for (int i = 0; i <huiyuan.length; i++) {
            if (huiyuan[i][0].equals(name)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
        	System.out.println("未找到该会员！");
        } else {
            System.out.print("请输入新的储值余额：");
            int balance = scanner.nextInt();
            System.out.print("请输入新的会员积分：");
            int points = scanner.nextInt();
            System.out.print("是否是会员（是/否）：");
            boolean isMember = scanner.next().equals("是");

           huiyuan[index][1] = balance;
           huiyuan[index][2] = points;
           huiyuan[index][3] = isMember;
            System.out.println("修改成功！");
        }

        System.out.println("回车返回上一级》》》");
        scanner.nextLine();
        memberManagement();
    }


    private static void xhz() {
     
    	System.out.println("悦茶点餐系统->主菜单->菜单管理");
    	System.out.println("1.显示所有菜单信息");
    	System.out.println("2.添加菜单");
    	System.out.println("3.修改菜单");
    	System.out.println("4.返回主菜单");
    	System.out.println("请选择，请输入数字");
    	int choice = getChoice();
    	 if (choice == 1) {
             one();
         } else if (choice == 2) {
            two();
         } else if (choice == 3) {
             three();
         } else if (choice == 4) {
             showMainMenu();
         }
    }
    private static void one() {
    	System.out.println("悦茶点餐系统->主菜单->菜单管理->显示所有菜单信息");
        System.out.println("菜单编号\t菜单名\t价格");
        for (Object[] membe : mean) {
            System.out.println(membe[0] + "\t" + membe[1] + "\t" + membe[2]);
        }
        System.out.println("回车返回上一级》》》");
        scanner.nextLine();
        xhz();
    }
    private static void two() {
    	System.out.println("悦茶点餐系统->主菜单->菜单管理->添加菜单");
        System.out.print("请输入菜单编号：");
        String ggg = scanner.next();
        System.out.print("请输入菜单名");
        String fff = scanner.next();
        System.out.print("请输入价格");
        int bbb = scanner.nextInt();
       

        Object[][] newhuiyuan = new Object[mean.length + 1][3];
        for (int i = 0; i < mean.length; i++) {
            newhuiyuan[i] = mean[i];
        }
        newhuiyuan[mean.length] = new Object[]{ggg, fff, bbb};
        mean = newhuiyuan;

        System.out.println("添加成功！");
        System.out.println("回车返回上一级》》》");
        scanner.nextLine();
        xhz();
    }
    	
    
    private static void three() {
    	System.out.println("悦茶点餐系统->主菜单->菜单管理->删除菜单");
        System.out.print("请输入要删除的菜单编号：");
        String menuId = scanner.next();

        int index = -1;
        for (int i = 0; i < mean.length; i++) {
            if (mean[i][0].equals(menuId)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("未找到该菜单！");
        } else {
            Object[][] newmean = new Object[mean.length - 1][3];
            for (int i = 0; i < index; i++) {
                newmean[i] = mean[i];
            }
            for (int i = index + 1; i < mean.length; i++) {
                newmean[i - 1] = mean[i];
            }
            mean = newmean;

            System.out.println("删除成功！");
            System.out.println("回车返回上一级》》》");
            scanner.nextLine();
            xhz();
        }
    }
    private static void checkout() {
    	
    	    System.out.println("悦茶点单系统->主菜单->点单管理");
    	    System.out.println("商品菜单列表");

    	    for (Object[] membe : mean) {
    	        System.out.println(membe[0] + "\t" + membe[1] + "\t" +membe[2]);
    	    }
    	    System.out.print("请输入要选择的菜品编号：");
    	    String membeId = scanner.next();
    	    System.out.print("请选择当前的会员账户:");
    	    String memberId = scanner.next();

    	    Object[] selectedItem = null;
    	    for (Object[] membe : mean) {
    	        if (membe[0].equals(membeId)) {
    	            selectedItem = membe;
    	            break;
    	        }
    	    }
    	    if (selectedItem == null) {
    	        System.out.println("未找到该商品！");
    	        return;
    	    }
    	    System.out.println("查询到的商品：" + selectedItem[1] + "（" + selectedItem[0] + ")，价格：" + selectedItem[2]);
    	
    	    Object[] selectedMember = null;
    	    for (Object[] member :huiyuan) {
    	        if (member[0].equals(memberId)) {
    	            selectedMember = member;
    	            break;
    	        }
    	    }
    	    if (selectedMember == null) {
    	        System.out.println("未找到该会员！");
    	        return;
    	    }
    	    System.out.println("当前账户" + selectedMember[0] + " 余额为：" + selectedMember[1]);
    	    System.out.println("当前会员状态：" + ((boolean) selectedMember[3] ? "vip" : "普通"));
    	   
    	    int price = (int) selectedItem[2];
    	    if ((boolean) selectedMember[3]) {
    	        price *= 0.9;
    	    }
    	    System.out.println("实际需支付：" + price + "元");
    	    System.out.print("是否需要购买（y/n): ");
    	    String confirm = scanner.next();
    	    if (confirm.equals("y")) {
    	 
    	        int balance = (int) selectedMember[1];
    	        int points = (int) selectedMember[2];
    	        balance -= price;
    	        points += price;
    	        selectedMember[1] = balance;
    	        selectedMember[2] = points;
    	        System.out.println("恭喜您，购买成功！当前余额：" + balance + " 当前积分：" + points);
    	    }
    	    System.out.print("是否继续点单（y/n): ");
    	    String continueOrder = scanner.next();
    	    if (continueOrder.equals("n")) {
    	        showMainMenu();
    	    }
    	}
    private static void pointsConsumption() {
        System.out.println("悦茶点餐系统->主菜单->积分消费");
        System.out.println("积分商品列表：");
        System.out.println("礼品编号\t礼品名\t礼品所需积分");
        for (Object[] gift : liwu) {
            System.out.println(gift[0] + "\t" + gift[1] + "\t" + gift[2]);
        }
        System.out.print("请输入要兑换的礼品编号：");
        String giftId = scanner.next();
        int giftIndex = -1;
        for (int i = 0; i < liwu.length; i++) {
            if (liwu[i][0].equals(giftId)) {
                giftIndex = i;
                break;
            }
        }
        if (giftIndex == -1) {
            System.out.println("未找到该礼品！");
        } else {
            System.out.println("查询到礼品：" + liwu[giftIndex][1] + "（" + liwu[giftIndex][0] + ")，积分：" + liwu[giftIndex][2]);
            System.out.print("是否确认购买（y/n)：");
            String confirm = scanner.next();
            if (confirm.equals("y")) {
                System.out.print("请输入会员编号：");
                String memberId = scanner.next();
                int memberIndex = -1;
                for (int i = 0; i <huiyuan.length; i++) {
                    if (huiyuan[i][0].equals(memberId)) {
                        memberIndex = i;
                        break;
                    }
                }
                if (memberIndex == -1) {
                    System.out.println("未找到该会员！");
                } else {
                    int points = (int)huiyuan[memberIndex][2];
                    int giftPoints = (int) liwu[giftIndex][2];
                    if (points < giftPoints) {
                        System.out.println("积分不足！");
                    } else {
                        points -= giftPoints;
                       huiyuan[memberIndex][2] = points;
                        System.out.println("恭喜您，购买成功！当前余额：" + points);
                    }
                }
            }
        }
        System.out.println("enter回车返回主菜单》》》");
        scanner.nextLine();
        showMainMenu();
    }
    private static void logout() {
       
    	System.out.print("欢迎进入注销页面");
    }
    private static void exitSystem() {
        System.out.println("退出系统！");
    }
}
